$(function(){
    "use strict";
    // 当点击直接投入的时候
    $('.directInto_box>p').click(function(){
        $(this).siblings('ul.directInto_list').toggleClass('active')
    })
    //当点击核算确定的时候
    $('#check').on('click','.cancel_btn',function(){
        $('#check').removeClass('active')
    })
    // 点击出现搜索框
    $('.hidden_box_showBox').click(function(){
        $(this).siblings('.hidden_box_hiddenBox').toggleClass('active')
        if($(this).children('i').hasClass('icon-jiantou')){
            $(this).children('i').removeClass('icon-jiantou').addClass('icon-jiantou-top')
        }else{
            $(this).children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        }
    })
    // 当确定搜索时
    $('.hiddenBox_list_btn').on('click','button',function(){
        $('.hidden_box_hiddenBox').removeClass('active')
        $('.hidden_box_showBox').children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        if($(this).html() == '确定'){
            $('#dateVal').html(window.query_year_people||'全部')

            let time = $(this).closest('.hidden_box_hiddenBox').find('[data-query="time"]').val()
            let name = $(this).closest('.hidden_box_hiddenBox').find('[data-query="name"]').val()
            window.location.href = `/Admin/ProStaff/index?time=${time}&staff_name=${name}`

        }else{
            window.query_year_people = null
        }
    })
    //当点击查看记录时
    $('#table_table').on('click','.look_record_btn',function(e){
        e.preventDefault()
        $(this).siblings('.tip_hiddenBox').addClass('active')
    })
    // 当点击工时记录中确认按钮时
    $('#table_table').on('click','.work_record_box>.btn',function(e){
        e.preventDefault()
        $(this).parent().removeClass('active')
    })
    //全选的功能
    $('#checkAll').on('click',function(){
        let checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for(let i=0;i<checkboxs.length;i++){
            let checkbox=checkboxs[i];
            if(checkbox.disabled){
                // colnsole.log(11)
                continue
            }else{
                checkbox.checked=this.checked;
            }
        }
    })
    $('#table_table').on('click','input[type=checkbox]:not("#checkAll")',function(){
        "use strict";
        var isCheckedAll = true;
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                isCheckedAll = false;
                break;
            }
        }
        $('#checkAll')[0].checked = isCheckedAll;
    })

    /*当点击添加按钮时*/
    $('.operation_btn').on('click','a',function(e){
        e.preventDefault()
        if( $(this).html() == '添加' ){
            var add_people = $(this).attr('href')
            $(`[lll=${add_people}]`).addClass('active')
            // let htmls = '<option></option>';
            // $.ajax({
            //     url:'/Admin/Pay/getStaffName',
            //     type:'get',
            //     success:function(data){
            //         for(var i=0;i<data.length;i++){
            //             htmls += `
            //                 <option value="${data[i].id}" data-dep="${data[i].dep_name}">${data[i].staff_name}</option>
            //             `
            //         }
            //         $(`[lll=${add_people}]`).find('[add_name="add_name"]').html(htmls)
            //     }
            // })
            /*添加默认的1-30*/
        }
        // if( $(this).html() == '核算' ){
        //     var add_people = $(this).attr('href')
        //     var checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
        //     var len = 0;
        //     let hs_arr = [];
        //     for (var i = 0; i < checkboxs.length; i++) {
        //         if (checkboxs[i].checked) {
        //             continue
        //         }else{
        //             len++
        //         }
        //     }
        //     if(len == checkboxs.length){
        //         $('#yz_modal').html('您还没有选择记录！')
        //         $('#yz_modal').addClass('active')
        //         tipShow()
        //         return
        //     }else{
        //         for (let hsi = 0; hsi < checkboxs.length; hsi++) {
        //             if (checkboxs[hsi].checked) {
        //                 hs_arr.push( $(checkboxs[hsi]).closest('tr[data-index]').attr('data-index') )
        //             }else{
        //                 continue;
        //             }
        //         }
        //         $.ajax({
        //             url:'/Admin/ProStaff/calCulStaff',
        //             type:'get',
        //             data:{
        //                 id:hs_arr
        //             },
        //             success:function(data){
        //                 if(data.fail==0){
        //                     window.location.href='/Admin/ProStaff/index'
        //                 }else if(data.code==0){
        //                     $('#check').find('.check_success_num').html(data.success)
        //                     $('#check').find('.check_error_num').html(data.fail)
        //                     $('#check').find('.check_error_Reason').html(data.msg)
        //                     $(add_people).addClass('active')
        //                 }else if(data.code==1){
        //                     if(data.fail==0){
        //                         $('#check').find('.check_success_num').html(data.success)
        //                         $('#check').find('.check_error_num').html(data.fail)
        //                         $('#check').find('.check_error_Reason').remove()
        //                         $(add_people).addClass('active')
        //                     }else{
        //                         $('#check').find('.check_success_num').html(data.success)
        //                         $('#check').find('.check_error_num').html(data.fail)
        //                         $('#check').find('.check_error_Reason').html(data.fail_why)
        //                         $(add_people).addClass('active')
        //                     }
        //                 }
        //             }
        //         })
        //     }
        //
        // }
        if( $(this).html() == '批量删除' ){
            var checkboxs =$('input[type=checkbox]:not("#checkAll")');
            var len = 0;
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择人员！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
            let _delete = $(this).attr('href');
            $(_delete).addClass('active')
        }
        if( $(this).html() == '批量导入' ){
            let showbox_id = $(this).attr('href');
            $(showbox_id).addClass('active')
        }
    })
    $(`[lll='add_people']`).find('[add_name="add_name"]').change(function(){
        let dep = $(this).children('option:selected').attr('data-dep')
        $(`[lll='add_people']`).find('[add_name="add_department"]').val(dep)
    })
    // 批量删除
    $('.delete_tip_btn_box').on('click','button',function(){
        "use strict";
        let delete_arr = [];
        if($(this).html() == '确认'){
            let checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            for (let i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    delete_arr.push( $(checkboxs[i]).closest('tr[data-index]').attr('data-index') )
                }else{
                    continue;
                }
            }
            $.ajax({
                url:'/Admin/ProStaff/multiDelStaff',
                type:'post',
                data:{
                    pro_staff_id: delete_arr
                },
                success:function(data){
                    if(data.code==1){
                        for(let r = 0; r < delete_arr.length; r++){
                            $('#table_table').children('tbody').children(`[data-index=${delete_arr[r]}]`).remove();
                        }
                        $('#delete').removeClass('active')
                        window.location.href = `/Admin/ProStaff/index`//强制刷新
                    }else{
                        $('#yz_modal').html(data.msg)
                        $('#yz_modal').addClass('active')
                        tipShow()
                        return
                    }
                    checkBox()
                }
            })
        }else{
            $('#delete').removeClass('active')
        }
    })
    // 当点击编辑按钮时
    $('#table_table').on('click','.edit_table',function(e){
        e.preventDefault()
        var operation = $(this).attr('href')
        if($(this).html()=='编辑'){
            let operation_id = $(this).closest('tr').attr('data-index');
            $.ajax({
                url:'/Admin/ProStaff/editStaff',
                type:'get',
                data:{
                    id:operation_id
                },
                success:function(data){
                    if(data.code==1){
                        let datas = data.data;
                        
                        $(`[lll=${operation}]`).find(`[add_name='start_time_edit']`).val(datas.start_time)
                        $(`[lll=${operation}]`).find(`[add_name='edit_name']`).val(datas.staff_name)
                        //类型
                        let type = datas.type
                        if(type=='外聘'){
                            $(`[lll=${operation}]`).find(`[add_name='edit_Insurance']`).attr('readonly','readonly')
                            $(`[lll=${operation}]`).find(`[add_name='edit_fund']`).attr('readonly','readonly')
                        }else if(type=='正式'){
                            $(`[lll=${operation}]`).find(`[add_name='edit_Insurance']`).removeAttr('readonly')
                            $(`[lll=${operation}]`).find(`[add_name='edit_fund']`).removeAttr('readonly')
                        }
                        let edit_typeList = $(`[lll=${operation}]`).find('[add_name="edit_type"]').children()
                        for(let typei=0;typei<edit_typeList.length;typei++){
                            let edit_typeVal = $(edit_typeList[typei]).children('span').html();
                            if(edit_typeVal == type){
                                $(edit_typeList[typei]).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
                                $(edit_typeList[typei]).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
                            }
                        }

                        $(`[lll=${operation}]`).find(`[add_name='edit_baseSalary']`).val(datas.prize)
                        $(`[lll=${operation}]`).find(`[add_name='edit_Insurance']`).val(datas.ins)
                        $(`[lll=${operation}]`).find(`[add_name='edit_fund']`).val(datas.jin)

                        window.sessionStorage['operation_id']=operation_id
                        $(`[lll=${operation}]`).addClass('active');
                    }
                }
            })
        }else{//当他等于取消核算的时候
            let cancel_hesuan_id = $(this).closest('tr[data-index]').attr('data-index')
            let that = this
            $.ajax({
                url: '/Admin/ProStaff/canelStaff',
                type: 'get',
                data: {
                    id: cancel_hesuan_id
                },
                success: function(data){
                    if(data.code==1){
                        console.log(1)
                        $(that).closest('tr[data-index]').find('input').removeAttr('disabled')
                        $(that).html('编辑')
                        $(that).attr('href','edit_people')
                        // window.
                    }
                }
            })
        }

    })
    $(`[lll='edit_people']`).find('[add_name="edit_name"]').change(function(){
        let dep = $(this).children('option:selected').attr('data-dep')
        $(`[lll='edit_people']`).find('[add_name="edit_department"]').val(dep)
    })

    $('#add_people').find('[add_name=add_type]').on('click','li',function(){
        let type=$(this).children('span').html()
        $(this).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
        $(this).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
        if(type=='外聘'){
            $('#add_people').find('[add_name=add_Insurance]').attr('readonly','readonly')
            $('#add_people').find('[add_name=add_Insurance]').val('0')
            $('#add_people').find('[add_name=add_fund]').attr('readonly','readonly')
            $('#add_people').find('[add_name=add_fund]').val('0')
        }else if(type=='正式'){
            $('#add_people').find('[add_name=add_Insurance]').removeAttr('readonly')
            $('#add_people').find('[add_name=add_fund]').removeAttr('readonly')
        }
    })

    // 确认按钮点击时
    $('#add_btn').click(function(){
        let start_time_add = $(this).closest('[lll]').find('[add_name=start_time_add]').val()/*时间*/
        // let add_name = $(this).closest('[lll]').find('[add_name=add_name] option:selected').text()/*人员名字*/
        let add_name = $(this).closest('[lll]').find('[add_name=add_name]').val()/*人员名字*/
        // let add_name_id = $(this).closest('[lll]').find('[add_name=add_name]').val()/*人员id*/
        let type = $(this).closest('[lll]').find('[add_name=add_type]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
        let prize = $(this).closest('[lll]').find('[add_name=add_baseSalary]').val()/*工资*/
        let ins = $(this).closest('[lll]').find('[add_name=add_Insurance]').val()/*五险*/
        let jin = $(this).closest('[lll]').find('[add_name=add_fund]').val()/*公积金*/

        if(type=='外聘'){
            if(start_time_add == '' || add_name == '' || prize == '' || type==''){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
        }else if(type=='正式'){
            if(start_time_add == '' || add_name == '' || prize == '' || type=='' || ins == '' || jin == ''){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
        }

        //防止重复点击按钮
        if( $(this).attr('lock') ) return
        $(this).attr('lock','lock')

        let that = this
        $.ajax({
            url:'/Admin/ProStaff/addStaff',
            type:'post',
            data:{
                start_time: start_time_add,
                staff_name: add_name,
                type:type,
                prize:prize,
                ins: ins,
                jin:jin
            },
            success:function(data){
                if(data.code==1){
                    let peopleTableHTML = `
                            <tr data-index="${data.id}">
                                    <td><input type="checkbox" class="checkboxStyle"></td>
                                    <td>${start_time_add}</td>
                                    <td>${add_name}</td>
                                    <td>${type}</td>
                                    <td>${prize}</td>
                                    <td>${ins}</td>
                                    <td>${jin}</td>
                                    <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                </tr>
                    `
                    $('#table_table').children('tbody').prepend(peopleTableHTML)
                    $(that).closest('[lll]').find('[add_name]').val('')
                    $(that).closest('[lll]').removeClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
                $(that).removeAttr('lock')
            }
        })

    })
    // 确认并新增按钮点击时
    $('#add_AddNew_btn').click(function(){
        let start_time_add = $(this).closest('[lll]').find('[add_name=start_time_add]').val()/*时间*/
        // let add_name = $(this).closest('[lll]').find('[add_name=add_name] option:selected').text()/*人员名字*/
        let add_name = $(this).closest('[lll]').find('[add_name=add_name]').val()/*人员名字*/
        // let add_name_id = $(this).closest('[lll]').find('[add_name=add_name]').val()/*人员id*/
        let type = $(this).closest('[lll]').find('[add_name=add_type]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
        let prize = $(this).closest('[lll]').find('[add_name=add_baseSalary]').val()/*工资*/
        let ins = $(this).closest('[lll]').find('[add_name=add_Insurance]').val()/*五险*/
        let jin = $(this).closest('[lll]').find('[add_name=add_fund]').val()/*公积金*/

        if(type=='外聘'){
            if(start_time_add == '' || add_name == '' || prize == '' || type==''){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
        }else if(type=='正式'){
            if(start_time_add == '' || add_name == '' || prize == '' || type=='' || ins == '' || jin == ''){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
        }

        //防止重复点击按钮
        if( $(this).attr('lock') ) return
        $(this).attr('lock','lock')

        let that = this
        $.ajax({
            url:'/Admin/ProStaff/addStaff',
            type:'post',
            data:{
                start_time: start_time_add,
                staff_name: add_name,
                type:type,
                prize:prize,
                ins: ins,
                jin:jin
            },
            success:function(data){
                if(data.code==1){
                    let peopleTableHTML = `
                            <tr data-index="${data.id}">
                                    <td><input type="checkbox" class="checkboxStyle"></td>
                                    <td>${start_time_add}</td>
                                    <td>${add_name}</td>
                                    <td>${type}</td>
                                    <td>${prize}</td>
                                    <td>${ins}</td>
                                    <td>${jin}</td>
                                    <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                </tr>
                    `
                    $('#table_table').children('tbody').prepend(peopleTableHTML)
                    $(that).closest('[lll]').find('[add_name]').val('')
                    // $(that).closest('[lll]').removeClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
                $(that).removeAttr('lock')
            }
        })
    })

    $('#edit_people').find('[add_name=edit_type]').on('click','li',function(){
        let type=$(this).children('span').html()
        $(this).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
        $(this).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
        if(type=='外聘'){
            $('#edit_people').find('[add_name=edit_Insurance]').attr('readonly','readonly')
            $('#edit_people').find('[add_name=edit_Insurance]').val('0')
            $('#edit_people').find('[add_name=edit_fund]').attr('readonly','readonly')
            $('#edit_people').find('[add_name=edit_fund]').val('0')
        }else if(type=='正式'){
            $('#edit_people').find('[add_name=edit_Insurance]').removeAttr('readonly')
            $('#edit_people').find('[add_name=edit_fund]').removeAttr('readonly')
        }
    })
    // 保存按钮点击时
    $('#save_btn').click(function(){
        let time = $(this).closest('[lll]').find('[add_name=start_time_edit]').val()/*时间*/
        let name = $(this).closest('[lll]').find('[add_name=edit_name]').val()/*人员名字*/
        let type = $(this).closest('[lll]').find('[add_name=edit_type]').children().children('i.icon-Raidobox-selectedRai').siblings('span').html()
        let prize = $(this).closest('[lll]').find('[add_name=edit_baseSalary]').val()/*工资*/
        let ins = $(this).closest('[lll]').find('[add_name=edit_Insurance]').val()/*五险*/
        let jin = $(this).closest('[lll]').find('[add_name=edit_fund]').val()/*公积金*/

        if(type=='外聘'){
            if(time == '' || name == '' || prize == '' || type == ''){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
        }else if(type=='正式'){
            if(time == '' || name == '' || prize == '' || type == '' || ins == '' || jin == ''){
                $('#yz_modal').html('您有内容没有填写完整！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
        }
        


        let operation_id = window.sessionStorage['operation_id']
        let that = this
        $.ajax({
            url:'/Admin/ProStaff/editStaff',
            type:'post',
            data:{
                id:operation_id,
                start_time: time,
                staff_name: name,
                type:type,
                prize:prize,
                ins: ins,
                jin:jin
            },
            success:function(data){
                if(data.code==1){
                    let HTML = ''
                    HTML = `
                            <td><input type="checkbox" class="checkboxStyle"></td>
                            <td>${time}</td>
                            <td>${name}</td>
                            <td>${type}</td>
                            <td>${prize}</td>
                            <td>${ins}</td>
                            <td>${jin}</td>
                            <td style><a href="edit_people" class="edit_table">编辑</a></td>
                    `
                    $('#table_table').children('tbody').find(`tr[data-index=${operation_id}]`).html(HTML)
                    $(that).parent().parent().removeClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
            }
        })


    })
    // 取消按钮点击时
    $('.button_list').on('click','button',function(){
        if($(this).hasClass('cancel_btn')){
            $(this).closest('[lll]').find('[add_name]').val('')
            $(this).closest('[lll]').removeClass('active')
        }
    })

    let msg = '文件正在上传，请勿重复点击！'

    // 上传文件
    $('#file_upload').change(function (e) {
        $(this).siblings('.import_tip_btn_box').find('button').removeClass('btn-grey').addClass('btn-success')
        msg = '文件正在上传，请勿重复点击！'
        $(this).siblings('.import_tip_btn_box').find('button').removeAttr('lock')
    })
    $('#import').find('button').click(function(e){
        "use strict";
        e.preventDefault()
        if($(this).hasClass('btn-success')){
            if($(this).html()=='确定导入'){
                //防止重复点击按钮
                if( $(this).attr('lock') ){
                    $('#yz_modal').html(msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
                $(this).attr('lock','lock')
                let that = this

                var formData = new FormData($("#file_form")[0]);
                $.ajax({
                    async : false,
                    cache : false,
                    type : "post",
                    data : formData,
                    url : '/Admin/ProStaff/importStaff',
                    dataType : 'json',
                    contentType: false, //必须
                    processData: false, //必须
                    success : function(data) {
                        if(data.code==1){
                            $('#import').removeClass('active')
                            window.location.href = `/Admin/ProStaff/index`//强制刷新
                            $(that).removeAttr('lock')
                        }else{
                            msg = data.msg
                            $('#yz_modal').html(msg)
                            $('#yz_modal').addClass('active')
                            tipShow()
                        }
                    }
                })
            }
        }

    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox) 
        },2000)
    }
    //关闭所有已选择的框框
    function checkBox(){
        var checkboxs =$('input[type=checkbox]');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                continue;
            }else{
                $(checkboxs[i]).attr('checked',false)
            }
        }
    }

    //当点击区域不在导入弹框中时则关闭弹框
    $(document).click(function(e){
        let $this = $(e.target);
        if($this.closest('#import').length==0){
            if($this.attr('href') != '#import' && $this.children().attr('href') != '#import'){
                $('#import').removeClass('active');
            }
        }
    })

    //拖动拖动拖动
        let add_box =document.getElementById("add_people");
        let edit_box =document.getElementById("edit_people");
        add_box.onmousedown = function(ev){
            if($(ev.toElement).attr('id')=='add_people'){
                ev =ev ||event;
                let oLeft = add_box.offsetLeft;
                let oTop = add_box.offsetTop;
                let a =ev.clientX-oLeft;
                let b =ev.clientY-oTop;
                add_box.onmousemove =function (ev){
                    ev = ev || event;
                    add_box.style.left =ev.clientX-a+'px';
                    add_box.style.top =ev.clientY-b+'px';
                    $(add_box).css({'cursor':'default'})
                }
            }
            
        }
        edit_box.onmousedown = function(ev){
            if($(ev.toElement).attr('id')=='edit_people'){
                ev =ev ||event;
                let oLeft = edit_box.offsetLeft;
                let oTop = edit_box.offsetTop;
                let a =ev.clientX-oLeft;
                let b =ev.clientY-oTop;
                edit_box.onmousemove =function (ev){
                    ev = ev || event;
                    edit_box.style.left =ev.clientX-a+'px';
                    edit_box.style.top =ev.clientY-b+'px';
                    $(edit_box).css({'cursor':'default'})
                }
            }
        
        }
        document.onmouseup =function(){
            add_box.onmousemove =function(){
                null;
            }
            edit_box.onmousemove =function(){
                null;
            }
        }

})